<?php

namespace App\Common\WX\Api\msg48;


use App\Common\Log;
use App\Common\WX\Api\msg48\Entity\News;
use App\Common\WX\Api\OfficialAccount;
use XWX\Common\WebClient\WebClient;

class Msg48
{
    public function __construct(OfficialAccount $officialAccount)
    {
        $this->pub_officialAccount = $officialAccount;
    }

    private $pub_officialAccount;

    /**
     * @return OfficialAccount
     */
    protected function getOfficialAccount()
    {
        return $this->pub_officialAccount;
    }


    public function funcSendText($wx_from, $content)
    {
        $data = [
            'touser' => $wx_from,
            'msgtype' => 'text',
            'text' => [
                'content' => $content,
            ],
        ];

        return $this->funcSend($data);
    }

    public function funcSendImage($wx_from, $media_id)
    {
        $data = [
            'touser' => $wx_from,
            'msgtype' => 'image',
            'image' => [
                'media_id' => $media_id,
            ],
        ];

        return $this->funcSend($data);
    }

    public function funcSendNews(News $news)
    {
        $data = $news->toArray();
        Log::funcLogDebug(__METHOD__, $data);

        return $this->funcSend($data);
    }


    protected function funcSend($data)
    {
        $api_url = $this->getOfficialAccount()->funcGetApiUrl("cgi-bin/message/custom/send");


        $url = "{$api_url}?access_token={$this->getOfficialAccount()->config()->access_token}";
        $r = $this->getOfficialAccount()->funcApi($url, WebClient::METHOD_POST, $data);


        return $r;
    }
}